﻿/*
版权所有：版权所有(C) 2010，刘建新
文件编号：M01_MyallchildBll.cs
文件名称：MyallchildBll.cs
系统编号：Z0003007
系统名称：通用办公系统
模块编号：M01
模块名称：信息发布
设计文档：IOA_M01信息发布XDE建模.mdx
完成日期：2010-12-25
作　　者：
内容摘要：表[MyAllChild]业务操作类
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Eis51.CampanyCMS.Model.Bus;
using System.Data;
using Eis51.CampanyCMS.Access.Bus;
using System.Collections;

namespace Eis51.CampanyCMS.Business.Bus
{
    /// <summary>
	/// Myallchild的BLL 自定义方法
	/// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
	/// </summary>
    public partial class MyallchildBLL : BaseBLL<Myallchild>
    {


        public void ChangeParent(int Seqno,int NewParentid,string Type)
        {
            string sql = @"delete from MyAllChild where (Mychildid in (select Mychildid  from MyAllChild where mainid={0}) or Mychildid ={0}   ) and mainid in  (select mainid from MyAllChild where Mychildid={0} )  ";
            sql = string.Format(sql, Seqno);
            this.Execute(sql);
            DataTable MyChild = GetMyChild(Seqno);
            DataRow temp = MyChild.NewRow();
            temp["Mainid"] = 0;
            temp["Mychildid"] = Seqno;
            temp["Type"] = Type;
            MyChild.Rows.Add(temp);
            DataTable NewParent = GetMyParent(NewParentid);
            if (NewParentid > 0)
            {
                DataRow temp1 = NewParent.NewRow();
                temp1["Mainid"] = NewParentid;
                temp1["Mychildid"] = 0;
                temp1["Type"] = Type;
                NewParent.Rows.Add(temp1);
            }
            foreach(DataRow  dr  in NewParent.Rows )
            {
                foreach (DataRow drchil in MyChild.Rows)
                {
                    Myallchild m = new Myallchild();
                    m.Type = Type;
                    m.Mainid =int.Parse( dr["Mainid"].ToString()) ;
                    m.Mychildid =int.Parse ( drchil["Mychildid"].ToString());
                    this.Insert(m);
                }
            }
        }

        public void DeleteMyRelation(int Seqno)
        {
            string sql = @"delete from MyAllChild where (Mychildid in (select Mychildid  from MyAllChild where mainid={0}) or Mychildid ={0}   )     ";
            sql = string.Format(sql, Seqno);
            this.Execute(sql);
        }

        public  void AddParent(int childid, int NewParentid, string Type)
        {
            DataTable NewParent = GetMyParent(NewParentid);
            DataRow temp1 = NewParent.NewRow();
            temp1["Mainid"] = NewParentid;
            temp1["Mychildid"] = 0;
            temp1["Type"] = Type;
            NewParent.Rows.Add(temp1);
            foreach (DataRow dr in NewParent.Rows)
            {
                Myallchild m = new Myallchild();
                m.Type = Type;
                m.Mainid = int.Parse(dr["Mainid"].ToString());
                m.Mychildid = childid;
                this.Insert(m);
            }
        }

        public DataTable GetMyChild(int Seqno)
        {
            string sql = @"select  *  from MyAllChild where mainid={0} ";
            sql = string.Format(sql, Seqno);
            return  this.GetDataTable(sql);
        }

        public DataTable GetMyParent(int Seqno)
        {
            string sql = @"select *  from MyAllChild where Mychildid={0}";
            sql = string.Format(sql, Seqno);
            return this.GetDataTable(sql);
        }
    }

    #region 代码自动生成
    /// <summary>
    /// Myallchild的BLL
    /// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
    /// </summary>
    public partial class MyallchildBLL : BaseBLL<Myallchild>
    {
        #region 自动生成代码：取得相关实体

        /// <summary>
        /// 方法名称: MyallchildBLL
        /// 内容摘要: 构造函数进行初始化
        /// </summary>
        protected MyallchildBLL()
        {
        }
        private static volatile MyallchildBLL m_instance = null;
        /// <summary>
        /// 方法名称: GetInstance
        /// 内容摘要: 取得 MyallchildBLL 对象
        /// </summary>
        /// <returns>返回一个已经存在的实体</returns>
        public static MyallchildBLL GetInstance()
        {
            // 通用的必要代码 iBatisNet双校检机制,如果实例不存在
            if (m_instance == null)
            {
                lock (typeof(MyallchildBLL))
                {

                    // 如果实例不存在
                    if (m_instance == null)
                    {

                        // 创建一个的实例
                        m_instance = new MyallchildBLL();
                    }
                }
            }

            // 返回业务逻辑对象
            return m_instance;
        }

        #endregion

        #region 自动生成代码：基本业务公开函数

        /// <summary>
        /// 方法名称: Get
        /// 内容摘要: 由键值获取一个实体对象
        /// </summary>
        /// <returns>Myallchild</returns>
        public Myallchild Get(Myallchild.Key key)
        {

            return MyallchildDao.GetInstance().SelectByID(key);
        }
        /// <summary>
        /// 方法名称: Select
        /// 内容摘要: 基本查询，不带任何条件的查询
        /// </summary>
        public IList Select()
        {
            return MyallchildDao.GetInstance().SelectAll();
        }
        /// <summary>
        /// 方法名称: Insert
        /// 内容摘要: 插入一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Insert(Myallchild obj)
        {
            int result = 0;
            try
            {
                result = MyallchildDao.GetInstance().Insert(obj);
            }
            catch (Exception e)
            {
                throw e;
            }
            return result;
        }

        /// <summary>
        /// 方法名称: Update
        /// 内容摘要: 更新一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Update(Myallchild obj)
        {
            MyallchildDao.GetInstance().Update(obj);
            return 1;
        }

        /// <summary>
        /// 方法名称: Delete
        /// 内容摘要: 删除一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Delete(Myallchild obj)
        {
            bool result = MyallchildDao.GetInstance().DeleteByID(new Myallchild.Key(obj.Seqno));
            return result ? 1 : 0;
        }

        #endregion

    }
    #endregion
}